<?php
namespace App\Modules\Relationship\Controllers;

use App\Http\Controllers\BaseController;
use App\Http\Model\Hint;
use App\Http\Model\OutJson;
use App\Http\Services\UserServices;
use App\Lib\Response;
use Illuminate\Http\Request;
use App\Modules\Relationship\Services\FriendsService;

class FriendsController extends BaseController
{
    private $userService;
    private $friendsService;

    public function __construct()
    {
        $this->userService = new UserServices();
        $this->friendsService = new FriendsService();
    }

    public function getFriendList(Request $request) {
        $pageIndex = $request->get("pageIndex") ?? 0;
        $pageSize = $request->get("pageSize") ?? 10;

        $userInfo = $this->getUserInfo();
        $return = $this->friendsService->getFriendList($userInfo['user_id'],  $pageIndex, $pageSize);
        Response::returnResult(0, $return);
    }

    // 获取好友之间亲密关系和相应属性
    public function getRelationship (Request $request) {
        $userInfo = $this->getUserInfo();
        $friendId = $request->get("friendId");
        $return = $this->friendsService->getRelationship($userInfo, $friendId);
        Response::returnResult(0, $return);
    }

    // 获取互发消息计数
    public function reportChatNum (Request $request) {
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['user_id'];
        $friendId = $request->get('friendId');
        $num = (int) $request->get('num');
        if ($num < 1) {
            $num = 1;
        }
        $re = $this->friendsService->reportChatNum($userId, $friendId, $num);

        Response::returnResult(Hint::SUCCESS, $re['code'], $re['data'], $re['msg']);
    }

    private function getUserInfo () : array {
        $userInfo = $this->userService->get_current_user();
        if (!$userInfo) {
            OutJson::outJsonNoData(Hint::LOGIN_OUT, []);
        }
        return $userInfo;
    }

}